package sz.szsmk.citizencard.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.text.DecimalFormat;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.SearchAllViewBinding;
import sz.szsmk.citizencard.databinding.SearchServiceViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.SimpleRes;
import sz.szsmk.citizencard.ui.activity.SearchAllActivity;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/10 21:00
 * description:
 */
public class SearchTypeAdapter extends BaseRecyclerAdapter<BaseHolder> {
    private SearchAllActivity mContext;
    private List<SimpleRes.ResultBean.DetailBean> viewData;
    private int type ;//1 隐藏，0 展示
    private int datatype;

    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view,int position);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }
    public SearchTypeAdapter(SearchAllActivity searchAllActivity) {
        mContext = searchAllActivity;
    }

    @Override
    public int getItemViewType(int position) {

            return R.layout.search_all_view;


    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(datatype==1){
            return SearchServiceHolder.creat(parent);
        }else{
            return SearchTypeHolder.creat(parent);
        }

    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        if(datatype==1){
            loadItemDataService(position,SearchServiceHolder.servicebinding);
        }else{
            loadItemData(position,SearchTypeHolder.binding);
        }

    }

    private void loadItemData(final int position, SearchAllViewBinding binding) {
        if (viewData != null) {
            SimpleRes.ResultBean.DetailBean detailBean = viewData.get(position);

                binding.businessIv.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(detailBean.getPic_url()).into(binding.businessIv);
                binding.businessShop.setTextSize(15);
                binding.businessShop.setTextColor(0xff3D494F);


            binding.businessShop.setText(detailBean.getName());
            binding.businessAdress.setText(detailBean.getAddress());
            DecimalFormat df = new DecimalFormat("#.00");
            String dis =detailBean.getDistance();
            if(datatype==4){
                binding.linkphone.setVisibility(View.VISIBLE);
                binding.linkphone.setText(detailBean.getPhone());

            }else{
                binding.linkphone.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(dis)) {
                long dislong = Long.parseLong(dis);
                binding.businessDistance.setVisibility(View.VISIBLE);
//                if(dislong/1000>100){
//
//                    int tempdis = (int) (dislong/1000);
//                    dis= tempdis+"";
//                }else{
                    dis = df.format((float)dislong/1000);
//                }
                binding.businessDistance.setText("距您"+dis+"km");
            } else {
                binding.businessDistance.setVisibility(View.GONE);
            }


            if(datatype==4){
                binding.distanceImg.setVisibility(View.GONE);
                binding.businessDistance.setVisibility(View.VISIBLE);
                binding.businessDistance.setText(detailBean.getDesc());
            }else{
                if(type==1){
                    binding.businessDistance.setVisibility(View.GONE);
                    binding.distanceImg.setVisibility(View.GONE);
                }else{
                    binding.businessDistance.setVisibility(View.VISIBLE);
                    binding.distanceImg.setVisibility(View.VISIBLE);
                }
            }
//            binding.businessDistance.setText("距您" + detailBean.getDistance());
            binding.businessRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    baseInterface.onClick(v,position);
                }
            });
        }

    }

    private void loadItemDataService(final int position, SearchServiceViewBinding servicebinding) {
        if (viewData != null) {
            SimpleRes.ResultBean.DetailBean detailBean = viewData.get(position);


                Glide.with(mContext).load(detailBean.getPic_url()).into(servicebinding.businessSmalliv);
            servicebinding.businessShop.setTextSize(14);
            servicebinding.businessShop.setTextColor(0xff637077);
            servicebinding.businessShop.setText(detailBean.getName());


//            binding.businessDistance.setText("距您" + detailBean.getDistance());
            servicebinding.businessRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    baseInterface.onClick(v,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
//        return 3;
    }

    public void setViewData(List<SimpleRes.ResultBean.DetailBean> viewData,int type,int datatype) {
        this.viewData = viewData;
        this.type = type;
        this.datatype= datatype;
    }

    public static class SearchTypeHolder extends BaseHolder {

        private static SearchAllViewBinding binding;

        public static SearchTypeHolder creat(ViewGroup parent) {
            SearchAllViewBinding binding = SearchAllViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new SearchTypeHolder(binding);
        }

        public SearchTypeHolder(SearchAllViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public static class SearchServiceHolder extends BaseHolder {

        private static SearchServiceViewBinding servicebinding;

        public static SearchServiceHolder creat(ViewGroup parent) {

            SearchServiceViewBinding binding = SearchServiceViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new SearchServiceHolder(binding);
        }

        public SearchServiceHolder(SearchServiceViewBinding binding) {
            super(binding.getRoot());
            this.servicebinding = binding;
        }
    }
}
